home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 16 / sets / setsourc / set_test.c < prev    next >
C/C++ Source or Header  |  1989-03-01  |  9KB  |  325 lines

  1. /* set_test.c  --  for testing set library
  2.  
  3.             by A. S. Cherdak, 12/26/88
  4.             written for Turbo-C, Version 2.0
  5.  
  6.      DISCLAIMER - DISCLAIMER - DISCLAIMER - DISCLAIMER - DISCLAIMER
  7.       Use at your own risk, The author is NOT responsible for ANY
  8.      damages resulting from the use, proper or improper, of this
  9.      software.
  10.  
  11.  
  12. This program is a do-little-to-nothing code for demonstrating applications
  13. of the functions in the set library.  Throughout this code, there are usually
  14. several applications of the same set function.  This was done to provide a
  15. bit of variety and to show that there are more than one way to use these
  16. functions.  The functions included in the set library are those needed to
  17. implement as much of the standard Pascal set functionality as possible at
  18. this time.
  19.  
  20. */
  21.  
  22. #include <stdio.h>
  23. #include "sets.h"
  24. defset(responses,CHARACTER,256,0);
  25. typedef enum {apples,oranges,grapes,pears,peaches} fruit;
  26.  
  27. /*************************************************************************/
  28.                            void example1()
  29. /*************************************************************************/
  30. {
  31. /* Example - 1:  Adapted From Standard Pascal User Reference Manual --
  32.    Subtracts characters from a set and prints the remaining characters.
  33. */
  34. char c;
  35. defset(alphabet,CHARACTER,256,0);  /* define two sets in automatic storage */
  36. defset(temp,CHARACTER,256,0);
  37.  
  38. /* Load one set with upper case alphabet */
  39. if(set_of(&alphabet,'A'__'Z',_E) != SUCCESS)
  40.     {
  41.     printf("Failure creating set = \"alphabet\"!");
  42.     exit(1);
  43.     }
  44.  
  45. /* clear temporary set */
  46. set_clear(&temp);
  47.  
  48. c = '\0';
  49. printf("\n\n\n********************* example1 **************************");
  50. printf("\nADD AND DELETE CHARACTERS FROM A SET OF 'A'__'Z'.  DISPLAY DELETED");
  51. printf("\nAND ADDED CHARACTERS AND THE REMAINING CHARACTERS IN THE SET.");
  52. printf("\nUses set_difference(), set_of(), set_print(), defset(),");
  53. printf("\nset_clear(), and add_member().\n");
  54. printf("\nEnter characters to be deleted from\n");
  55. set_print(&alphabet);
  56. printf("\n");
  57.  
  58. do    {
  59.     /* PRINT DELETED CHARACTERS */
  60.     printf("%c",c);
  61.  
  62.     /* get character to be deleted from alphabet */
  63.     c = getch();
  64.  
  65.     /* add character to temp set */
  66.     if(add_member(&temp,c) == FAILURE)
  67.         {
  68.         printf("\nFailure creating set \"temp\"!");
  69.         exit(1);
  70.         }
  71.     }
  72. while(c != 0x0d);
  73. printf("  DELETED\n");
  74. if(set_difference(&temp,&alphabet,&temp) != SUCCESS)
  75.     {
  76.     printf("\nFailure in set_difference!");
  77.     exit(1);
  78.     }
  79. set_print(&temp);
  80.  
  81. }  /* end example1 */
  82.  
  83. /*************************************************************************/
  84.                            void example2()
  85. /*************************************************************************/
  86. {
  87. defset(seta,CHARACTER,256,0);
  88. defset(setb,CHARACTER,256,0);
  89. defset(setc,CHARACTER,256,0);
  90.  
  91.     printf("\n\n\n********************* example2 **************************");
  92.     printf("\nCREATE TWO OVERLAPPING SETS.  TAKE THE INTERSECTION OF THEM");
  93.     printf("\nDUMP THE SETS AND PRINT THE CONTENTS OF THE INTERSECTION.");
  94.     printf("\nUses set_of(), dump_set(), set_print(), set_intersect(),");
  95.     printf("\nand kill_set().");
  96.  
  97.     if(set_of(&seta,'A'__'Z','a'__'z',_E) == FAILURE)
  98.         {                                     
  99.         printf("\nFailure populating seta!");
  100.         exit(1);
  101.         }
  102.     if(set_of(&setb,'Z'__'b',_E) == FAILURE)
  103.         {
  104.         printf("\nFailure populating setb!");
  105.         exit(1);
  106.         }
  107.  
  108.     printf("\nSets:");
  109.     dump_set(&seta);
  110.     set_print(&seta);
  111.     dump_set(&setb);
  112.     set_print(&setb);
  113.     printf("\nIntersection set:");
  114.     if(set_intersect(&setc,&seta,&setb) == FAILURE)
  115.         {
  116.         printf("\nFailure intersecting sets!");
  117.         exit(1);
  118.         }
  119.     dump_set(&setc);
  120.     set_print(&setc);
  121.  
  122. }  /* end example2 */
  123.  
  124.  
  125. /*************************************************************************/
  126.                            void example3()
  127. /*************************************************************************/
  128. {
  129. defset(seta,CHARACTER,256,0);
  130. defset(setb,CHARACTER,256,0);
  131. defset(setc,CHARACTER,256,0);
  132.  
  133.     printf("\n\n\n********************* example3 **************************");
  134.     printf("\nCREATE TWO NON-OVERLAPPING SETS.  TAKE THE UNION OF THEM");
  135.     printf("\nDUMP THE SETS AND PRINT THE CONTENTS OF THE UNION.");
  136.     printf("\nUses set_of(), dump_set(), print_set(), and set_union().");
  137.  
  138.     if(set_of(&seta,'A'__'Z',_E) == FAILURE)
  139.         {
  140.         printf("\nFailure populating seta!");
  141.         exit(1);
  142.         }
  143.     if(set_of(&setb,'a'__'z',_E) == FAILURE)
  144.         {
  145.         printf("\nFailure populating setb!");
  146.         exit(1);
  147.         }
  148.  
  149.     printf("\nSets:");
  150.     dump_set(&seta);
  151.     set_print(&seta);
  152.     dump_set(&setb);
  153.     set_print(&setb);
  154.     printf("\nUnion set:");
  155.     if(set_union(&setc,&seta,&setb) == FAILURE)
  156.         {
  157.         printf("\nFailure in set_union!");
  158.         exit(1);
  159.         }
  160.  
  161.     dump_set(&setc);
  162.     set_print(&setc);
  163.  
  164. }  /* end example3 */
  165.  
  166.  
  167. /*************************************************************************/
  168.                               void example4()
  169. /*************************************************************************/
  170. {
  171. defset(A,CHARACTER,256,0);
  172. defset(B,CHARACTER,256,0);
  173. defset(C,ENUMERATED,5,0);
  174. defset(D,ENUMERATED,5,0);
  175.  
  176.     printf("\n\n\n********************* example4 **************************");
  177.     printf("\nTEST FOR SET EQUALITY, SET INEQUALITY, AND SET INCLUSION.");
  178.     printf("\nCREATE FOUR SETS OF THE SAME TYPE SUCH THAT A = B AND C < D.");
  179.     printf("\nAPPLY THE TESTS AND DISPLAY THE SETS AND THE RESULTS.");
  180.     printf("\nUses set_equality(), set_inequality(), set_included_in(),");
  181.     printf("\nset_includes().");
  182.     printf("\n\n");
  183.  
  184.     /* set up the sets */
  185.  
  186.     if(set_of(&A,'A'__'Z',_E) == FAILURE)
  187.         {
  188.         printf("\nFailure populating set A!");
  189.         exit(1);
  190.         }
  191.     if(set_of(&B,'A'__'Z',_E) == FAILURE)
  192.         {
  193.         printf("\nFailure populating set B!");
  194.         exit(1);
  195.         }
  196.     if(set_of(&C,apples __ peaches,_E) == FAILURE)
  197.         {
  198.         printf("\nFailure populating set C!");
  199.         exit(1);
  200.         }
  201.     if(set_of(&D,oranges __ pears,_E) == FAILURE)
  202.         {
  203.         printf("\nFailure populating set D!");
  204.         exit(1);
  205.         }
  206.  
  207.     printf("\nSet A ");                /* test for equality 1 */
  208.     set_print(&A);
  209.     if(set_equality(&A,&B))
  210.         printf(" equals ");
  211.     else
  212.         printf(" doesn't equal ");
  213.     printf("Set B ");
  214.     set_print(&B);
  215.  
  216.  
  217.     printf("\nSet A ");                /* test for equality 2 */
  218.     set_print(&A);
  219.     if(set_equality(&A,&C))
  220.         printf(" equals ");
  221.     else
  222.         printf(" doesn't equal ");
  223.     printf("Set C ");
  224.     set_print(&C);
  225.  
  226.  
  227.     printf("\nSet C ");                /* test for inequality 1 */
  228.     set_print(&C);
  229.     if(set_inequality(&C,&D))
  230.         printf(" doesn't equal ");
  231.     else
  232.         printf(" equals ");
  233.     printf("Set D ");
  234.     set_print(&D);
  235.  
  236.  
  237.     printf("\nSet A ");                /* test for inequality 2 */
  238.     set_print(&A);
  239.     if(set_inequality(&A,&B))
  240.         printf(" doesn't equal ");
  241.     else
  242.         printf(" equals ");
  243.     printf("Set B ");
  244.     set_print(&B);
  245.  
  246.     printf("\nSet A ");                /* test for inclusion 1 */
  247.     set_print(&A);
  248.     if(set_included_in(&A,&C))
  249.         printf(" is included in ");
  250.     else
  251.         printf(" is not included in ");
  252.     printf("Set C ");
  253.     set_print(&C);
  254.  
  255.     printf("\nSet D ");                /* test for inclusion 2 */
  256.     set_print(&D);
  257.     if(set_included_in(&D,&C))
  258.         printf(" is included in ");
  259.     else
  260.         printf(" is not included in ");
  261.     printf("Set C ");
  262.     set_print(&C);
  263.  
  264.     printf("\nSet C ");                /* test for includes 1 */
  265.     set_print(&C);
  266.     if(set_includes(&C,&D))
  267.         printf(" includes ");
  268.     else
  269.         printf(" does not include ");
  270.     printf("Set D ");
  271.     set_print(&D);
  272.  
  273.     printf("\nSet D ");                /* test for includes 2 */
  274.     set_print(&D);
  275.     if(set_includes(&D,&C))
  276.         printf(" includes ");
  277.     else
  278.         printf(" does not include ");
  279.     printf("Set C ");
  280.     set_print(&C);
  281.  
  282. }  /* end example4 */
  283.  
  284.  
  285.  
  286. /*************************************************************************/
  287.                               void next(int i)
  288. /*************************************************************************/
  289. /* gets user response */
  290. {
  291. int yesno;
  292.  
  293. do {
  294.     printf("\nNext Example (Number %d) or Quit (N or Q): ",i);
  295.    while(!kbhit());
  296.     yesno = getch();
  297.     }
  298. while(! in_set(&responses,yesno));
  299.  
  300. if(toupper(yesno) == 'Q')
  301.     exit(0);
  302.  
  303. }  /* end next */
  304.  
  305.  
  306. /*************************************************************************/
  307.                                   main()
  308. /*************************************************************************/
  309. {
  310. if(set_of(&responses,'N','n','Q','q',_E) == FAILURE)
  311.     {
  312.     printf("\nFailure in setting up set of responses!");
  313.     exit(1);
  314.     }
  315.  
  316. next(1);
  317. example1();
  318. next(2);
  319. example2();
  320. next(3);
  321. example3();
  322. next(4);
  323. example4();
  324. }
  325.